﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class QualityOrderProcessor : BaseProcessor {

        private int _pageSize;
        private QualityOrderRepository _qualityOrderRepository;

        public QualityOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._qualityOrderRepository = new QualityOrderRepository(pageSize);
        }

        public QualityOrder FetchQualityOrderById(int qualityOrderId) {
            try {
                return this._qualityOrderRepository.FetchQualityOrderById(qualityOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定质检单失败！");
            }
        }

        public QualityOrder FetchQualityOrderBySuspenseId(int suspenseOrderId) {
            try {
                return this._qualityOrderRepository.FetchQualityOrderBySuspenseId(suspenseOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定质检单失败！");
            }
        }

        public List<QualityOrderDetailsViewModel> FetchQualityOrderDetailsViewModelList(int qualityOrderId) {
            try {
                return this._qualityOrderRepository.FetchQualityOrderByIdForView(qualityOrderId).QualityOrderDetails.Select(x => {
                    return new QualityOrderDetailsViewModel() {
                        Key = x.QualityOrderDetailsId,
                        MaterialCode = x.Material.Code,
                        SuspeseQuantity = x.SuspenseQuantity,
                        QualifiedQuantity = x.QualifiedQuantity,
                        UnQualifiedQuantity = x.UnQualifiedQuantity,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定质检单失败！");
            }
        }

        public List<QualityOrderViewModel> FetchQualityOrderViewModelList(QualityOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<QualityOrder> resultList = this._qualityOrderRepository.FetchQualityOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new QualityOrderViewModel() {
                        Key = x.QualityOrderId,
                        Code = x.Code,
                        SuspenseOrderCode = x.SuspenseOrder.Code,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.Status),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取质检单列表失败！");
            }
        }

        public void Insert(QualityOrder qualityOrder) {
            try {
                qualityOrder.QualityOrderId = Guid.NewGuid().GetHashCode();
                qualityOrder.CreatedDatetime = DateTime.Now;
                qualityOrder.Status = (int)AuditStatus.Waiting;
                qualityOrder.QualityOrderDetails.ToList().ForEach(x => {
                    x.CreatedBy = qualityOrder.CreatedBy;
                    x.CreatedDatetime = qualityOrder.CreatedDatetime;
                    x.QualityOrderDetailsId = Guid.NewGuid().GetHashCode();
                });
                this._qualityOrderRepository.Insert(qualityOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新质检单失败！");
            }
        }

        public void Update(QualityOrder qualityOrder) {
            try {
                QualityOrder oldQualityOrder = this.FetchQualityOrderById(qualityOrder.QualityOrderId);
                if (!oldQualityOrder.RowVersion.SequenceEqual(qualityOrder.RowVersion)) {
                    throw this.BuildException("更新质检单失败，数据不一致性错误！");
                }
                qualityOrder.ModifiedDatetime = DateTime.Now;
                this._qualityOrderRepository.UpdateQualityOrder(qualityOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新质检单失败！");
            }
        }

        public void Delete(QualityOrder qualityOrder) {
            try {
                this._qualityOrderRepository.Delete(qualityOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除质检单失败！");
            }
        }

        public void Audit(QualityOrder qualityOrder) {
            try {
                QualityOrder oldQualityOrder = this.FetchQualityOrderById(qualityOrder.QualityOrderId);
                if (!oldQualityOrder.RowVersion.SequenceEqual(qualityOrder.RowVersion)) {
                    throw this.BuildException("审核质检单失败，数据不一致性错误！");
                }
                qualityOrder.AuditedDatetime = DateTime.Now;
                this._qualityOrderRepository.UpdateQualityOrder(qualityOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核质检单失败！");
            }
        }

        public void Delete(params int[] qualityOrderIds) {
            try {
                this._qualityOrderRepository.DeleteMultipleQualityOrder(qualityOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除质检单失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._qualityOrderRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取质检单最大流水号失败！");
            }
        }
    }
}
